/**
 * 
 */
package gmu.drr.modelInst.simple;

import java.util.Collection;
import java.util.Iterator;

import cern.jet.random.AbstractContinousDistribution;
import cern.jet.random.engine.RandomEngine;

import gmu.drr.entity.ModelModel;
import gmu.drr.entity.NetEdge;
import gmu.drr.entity.NetNode;
import gmu.drr.entity.NetNodeActModel;

/**
 * @author RothmD01
 *
 */
public class NetNodeActSimple extends NetNodeActModel {
	private double lambda;
	private double rho;
	private double threshold;
	private Collection<NetEdge> pred;
	private AbstractContinousDistribution excDist = null;
	private AbstractContinousDistribution supDist = null;
	/**
	 * @param excDist the excDist to set
	 */
	public void setExcDist(AbstractContinousDistribution excDist) {
		this.excDist = excDist;
	}
	/**
	 * @param supDist the supDist to set
	 */
	public void setSupDist(AbstractContinousDistribution supDist) {
		this.supDist = supDist;
	}
	public double getLambda() {
		return lambda;
	}
	public void setLambda(double lambda) {
		this.lambda = lambda;
	}
	public double getRho() {
		return rho;
	}
	public void setRho(double rho) {
		this.rho = rho;
	}
	public double getThreshold() {
		return threshold;
	}
	public void setThreshold(double threshold) {
		this.threshold = threshold;
	}
	@Override
	public boolean evalActivation() {
		double edgeBump = 0;
		for( Iterator<NetEdge> i = pred.iterator(); i.hasNext();) {
			NetEdge e = i.next();
			NetNode n = e.getHeadNode();
			double w;
			w = ((NetEdgeWeightSimple)e.getNetEdgeWeightModel()).getWeight();
			if( n.isActivated() ) edgeBump += w;
		}
		
		if( this.getNetNode().isActivated() ) {
			boolean deact = false;
			if( edgeBump < threshold ) {
				deact = true;
			} else { // need to ask the rho question
				// TODO determine distribution for rho - currently assuming a uniform <blech>
				double rdraw = rho * supDist.nextDouble();
				if( (edgeBump-rdraw) < threshold ) deact = true;
			}
			if( deact ) {
				this.getNetNode().setNewActivated(false);
				return true;
			}
		} else { // this node is not currently activated
			boolean act = false;
			if( edgeBump > threshold ) {
				act = true;
			} else { // need to ask the lambda question
				// TODO determine distribution for lambda - currently assuming a uniform <blech>
				double ldraw = lambda * excDist.nextDouble();
				if( (edgeBump+ldraw) > threshold ) act = true;
			}
			if( act ) {
				this.getNetNode().setNewActivated(true);
				return true;
			}
		}
		// if no change is made, return a negative
		return false;
	}
	
}
